//
//  WYCDetailCardController.m
//  信用卡管理
//
//  Created by ZX on 2017/11/22.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCDetailCardController.h"
#import "CustomPickerView.h"
#import "WYCDayPickerView.h"
#import "PickerDateViewController.h"
#import "MyTextField.h"
#import "RSAUtil.h"
#import "WYCBillController.h"
#import "WYCRepaymentController.h"
#import "WYCCodeViewController.h"
@interface WYCDetailCardController ()
{
    PickerDateViewController * pickerView;
    UIButton * button;
    MyTextField * mytexField;
}

@property (nonatomic,strong) UITextField *CVN2TextField;

@property (nonatomic,strong) UITextField *iphoneTexField;

/** 账单日*/
@property(nonatomic,strong)UITextField * check;

@property (nonatomic,strong) UITextField *QuotaTexField;
@property(nonatomic,strong)NSString  *cardNum;
@property(nonatomic,strong)NSString  *bankName;
@property(nonatomic,strong)NSString  *cardId;
@property(nonatomic,strong)NSString  *chuan;


@end

@implementation WYCDetailCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WYCGlobalBg;
    
    [self setupCreat];
    
    
    
    
}

- (void)setupCreat {
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:@"dic.plist"];
    //    //准备要存入的字典
    //    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.cardNumber,@"cardNumber",self.bankName,@"name",self.myteXfield.text,@"iphonenumeber",nil];
    //    //将字典存入指定的本地文件
    //    [dic writeToFile:documnetPath atomically:YES];
    //可对已经存储的字典操作
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    NSLog(@"%@", documnetPath);
    /*** 姓名 ****/
    //姓名
    UILabel * nameLabel = [[UILabel alloc] init];
    nameLabel.frame = CGRectMake(40 * px, WYCClearW + NavHeight, 150 * px, WYCClearH);
    nameLabel.text = @"姓名:";
    nameLabel.font = MiddleLight;
    [self.view addSubview:nameLabel];
    
    //赋值控件
    UILabel * name = [[UILabel alloc] init];
    name.frame = CGRectMake(CGRectGetMaxX(nameLabel.frame) + WYCClearW, nameLabel.y, ScreenWidth / 2, nameLabel.height);
    name.text = [WYCAccountTool unarchiveuserName];
    name.font = MiddleLight;
    [self.view addSubview:name];
    
    UIView * nameview = [[UIView alloc] init];
    nameview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(nameLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    nameview.backgroundColor = WYCgrayColor;
    [self.view addSubview:nameview];
    
    
    /*** 卡号 ****/
    //卡号
    UILabel * namberLabel = [[UILabel alloc] init];
    namberLabel.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(nameview.frame) + WYCClearW, 150 * px, WYCClearH);
    namberLabel.text = @"卡号:";
    namberLabel.font = MiddleLight;
    [self.view addSubview:namberLabel];
    
    //赋值控件
    UILabel * namber = [[UILabel alloc] init];
    namber.frame = CGRectMake(CGRectGetMaxX(namberLabel.frame) + WYCClearW, namberLabel.y, ScreenWidth / 2, nameLabel.height);
    namber.text = [resultDic objectForKey:@"cardNumber"];
    _cardNum = [resultDic objectForKey:@"cardNumber"];
//    _cardNum =[resultDic objectForKey:@"lastNumber"];
    namber.font = MiddleLight;
    [self.view addSubview:namber];
    
    UIView * namberview = [[UIView alloc] init];
    namberview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(namberLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    namberview.backgroundColor = WYCgrayColor;
    [self.view addSubview:namberview];
    
    
    /*** 银行 ****/
    //银行
    UILabel * bankLabel = [[UILabel alloc] init];
    bankLabel.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(namberview.frame) + WYCClearW, 150 * px, WYCClearH);
    bankLabel.text = @"银行:";
    bankLabel.font = MiddleLight;
    [self.view addSubview:bankLabel];
    
    //赋值控件
    UILabel * bank = [[UILabel alloc] init];
    bank.frame = CGRectMake(CGRectGetMaxX(bankLabel.frame) + WYCClearW, bankLabel.y, ScreenWidth / 2, nameLabel.height);
    bank.text = [resultDic objectForKey:@"name"];
    bank.font = MiddleLight;
    _bankName =[resultDic objectForKey:@"name"];
    [self.view addSubview:bank];
    
    UIView * bankview = [[UIView alloc] init];
    bankview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(bankLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    bankview.backgroundColor = WYCgrayColor;
    [self.view addSubview:bankview];
    
    
    /*** 有效期 ****/
    //有效期
    UILabel * ValidityLabel = [[UILabel alloc] init];
    ValidityLabel.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(bankview.frame) + WYCClearW, 100, WYCClearH);
    ValidityLabel.text = @"有效期:";
    ValidityLabel.font = MiddleLight;
    [self.view addSubview:ValidityLabel];
    
    
    UILabel * fhlabel = [[UILabel alloc] init];
    fhlabel.frame = CGRectMake(ScreenWidth - 80 * px , ValidityLabel.y + 20 * px, 40 * px, 80 * px);
    fhlabel.text = @">";
    fhlabel.textColor = [UIColor blackColor];
    [self.view addSubview:fhlabel];
    
    
    //赋值控件
    button = [[UIButton alloc] init];
    button.frame = CGRectMake(ValidityLabel.frame.size.width+ValidityLabel.frame.origin.x , ValidityLabel.y, ScreenWidth -150, nameLabel.height);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(bottonClick) forControlEvents:UIControlEventTouchUpInside];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.titleLabel.font = MiddleLight;
    [self.view addSubview:button];
    
    
    UIView * Validityview = [[UIView alloc] init];
    Validityview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(ValidityLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    Validityview.backgroundColor = WYCgrayColor;
    [self.view addSubview:Validityview];
    
    
    /*** cvn2 ****/
    //有效期
    UILabel * cvn2Label = [[UILabel alloc] init];
    cvn2Label.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(Validityview.frame) + WYCClearW, 200  * px, WYCClearH);
    cvn2Label.text = @"CVN2:";
    cvn2Label.font = MiddleLight;
    [self.view addSubview:cvn2Label];
    
    //赋值控件
    UITextField *cvn2TexField  = [[UITextField alloc] init];
    cvn2TexField.frame = CGRectMake(CGRectGetMaxX(cvn2Label.frame) + WYCClearW, cvn2Label.y, ScreenWidth / 2, nameLabel.height);
    //    name.text = @"姓名:";
    cvn2TexField.placeholder = @"请输入CVN2码后三位";
    cvn2TexField.font = MiddleLight;
    [self.view addSubview:cvn2TexField];
    self.CVN2TextField= cvn2TexField;
    
    UIView * cvn2view = [[UIView alloc] init];
    cvn2view.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(cvn2Label.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    cvn2view.backgroundColor = WYCgrayColor;
    [self.view addSubview:cvn2view];
    
    
    /*** 手机号 ****/
    //手机号
    UILabel * iphoneLabel = [[UILabel alloc] init];
    iphoneLabel.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(cvn2view.frame) + WYCClearW, 200  * px, WYCClearH);
    iphoneLabel.text = @"手机号:";
    iphoneLabel.font = MiddleLight;
    [self.view addSubview:iphoneLabel];
    
    
    //赋值控件
    UITextField *iphoneTexField = [[UITextField alloc] init];
    iphoneTexField.frame = CGRectMake(CGRectGetMaxX(iphoneLabel.frame) + WYCClearW, iphoneLabel.y, ScreenWidth / 2, nameLabel.height);
    iphoneTexField.text = [resultDic objectForKey:@"iphonenumeber"];
    iphoneTexField.font = MiddleLight;
    [self.view addSubview:iphoneTexField];
    self.iphoneTexField = iphoneTexField;
    
    
    UIView * iphoneview = [[UIView alloc] init];
    iphoneview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(iphoneLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    iphoneview.backgroundColor = WYCgrayColor;
    [self.view addSubview:iphoneview];
    self.iphoneTexField = iphoneTexField;
    
    /*** 账单日 ****/
    //账单日
    UILabel * checkLabel = [[UILabel alloc] init];
    checkLabel.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(iphoneview.frame) + WYCClearW, 200  * px, WYCClearH);
    checkLabel.text = @"账单日:";
    checkLabel.font = MiddleLight;
    [self.view addSubview:checkLabel];
    
    
    UILabel * zdlabel = [[UILabel alloc] init];
    zdlabel.frame = CGRectMake(ScreenWidth - 80 * px , checkLabel.y + 20 * px, 40 * px, 80 * px);
    zdlabel.text = @">";
    zdlabel.textColor = [UIColor blackColor];
    [self.view addSubview:zdlabel];
    
    NSMutableArray *checkArray = [NSMutableArray new];
    for (int i = 0; i < 28; i ++) {
        [checkArray addObject:[NSString stringWithFormat:@"%02d日",i+1]];
    }
    //赋值控件
    UITextField * check = [[UITextField alloc] init];
    cvn2TexField.placeholder = @"请选择CVN2码";
    check.frame = CGRectMake(CGRectGetMaxX(iphoneLabel.frame) + WYCClearW, checkLabel.y, ScreenWidth / 2, nameLabel.height);
    
    WYCDayPickerView *DayPickerView = [[WYCDayPickerView alloc]initWithSourceData:checkArray confirmBlock:^(NSString *dayStr) {
        check.text = dayStr;
        [self.view endEditing:YES];
        
    } cancelBlock:^(NSString *dayStr) {
        
        [self.view endEditing:YES];
    }];
    
    
    DayPickerView.frame  = CGRectMake(0, 0, 100, 220);
    check.inputView = DayPickerView;
    
    check.font = MiddleLight;
    [self.view addSubview:check];
    self.check = check;
    
    
    UIView * checkview = [[UIView alloc] init];
    checkview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(checkLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    checkview.backgroundColor = WYCgrayColor;
    [self.view addSubview:checkview];
    
    
    /*** 信用卡额度 ****/
    //信用卡额度
    UILabel * QuotaLabel = [[UILabel alloc] init];
    QuotaLabel.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(checkview.frame) + WYCClearW, 360  * px, WYCClearH);
    QuotaLabel.text = @"信用卡额度:";
    QuotaLabel.font = MiddleLight;
    [self.view addSubview:QuotaLabel];
    
    //赋值控件
    UITextField *QuotaTexField = [[UITextField alloc] init];
    QuotaTexField.frame = CGRectMake(CGRectGetMaxX(QuotaLabel.frame) + WYCClearW, QuotaLabel.y, ScreenWidth / 2, nameLabel.height);
    //    name.text = @"姓名:";
    QuotaTexField.font = MiddleLight;
    QuotaTexField.placeholder = @"请输入信用卡额度";
    [self.view addSubview:QuotaTexField];
    self.QuotaTexField = QuotaTexField;
    
    
    UIView * Quotaview = [[UIView alloc] init];
    Quotaview.frame = CGRectMake(nameLabel.x, CGRectGetMaxY(QuotaLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    Quotaview.backgroundColor = WYCgrayColor;
    [self.view addSubview:Quotaview];
    
    
    UILabel * textLabel = [[UILabel alloc] init];
    textLabel.frame = CGRectMake(0, CGRectGetMaxY(Quotaview.frame) + 40 * px, ScreenWidth, 50 * px);
//    textLabel.text = @"为了保证信用卡有效性绑定将扣除5.00元手续费";
//    textLabel.font = smallLight;
    textLabel.textColor = [UIColor clearColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:textLabel];
    
    
    //提交按钮
    UIButton *SubmitButton = [[UIButton alloc] init];
    SubmitButton.frame = CGRectMake(40 * px, CGRectGetMaxY(textLabel.frame) + 80 * px, ScreenWidth - 80 * px, 150 * px);
    SubmitButton.layer.cornerRadius = 5;
    [SubmitButton addTarget:self action:@selector(submitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    SubmitButton.backgroundColor = WYCmainColor;
    [SubmitButton setTitle:@"提交" forState:UIControlStateNormal];
    [SubmitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:SubmitButton];
    
}
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_CVN2TextField resignFirstResponder];
    [_QuotaTexField resignFirstResponder];
    [_iphoneTexField resignFirstResponder];
}

- (void)bottonClick {
    
    __weak __typeof(self) weakself = self;
    pickerView =[[PickerDateViewController alloc]init];
    [self.view addSubview:pickerView.view];
    pickerView.selectedBlock = ^(NSString * selectedDate){
        NSString * year= [selectedDate substringFromIndex:4];
        NSString *mouth = [selectedDate substringToIndex:2];
        NSString  *date =[NSString stringWithFormat:@"%@/%@",mouth,year];
        _chuan = [NSString stringWithFormat:@"%@-%@",[selectedDate substringFromIndex:2],mouth];

        [weakself buttonTitle:date];
    };
    
}

- (NSString *)buttonTitle:(NSString *)title{
    NSLog(@"有效期%@",title);
  
    [button setTitle:title forState:UIControlStateNormal];
    
    
    return title;
}
- (void)submitButtonClick{
   
    if (self.CVN2TextField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入CVN2码"];
        return;
    }
    if (self.iphoneTexField.text.length != 11) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请正确手机号"];
        return;
    }
    if (self.QuotaTexField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入信用额度"];
        return;
    }
    if (self.check.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"账单日"];
        return;
    }
    
    // 正面沙盒
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documnetPath = [documents stringByAppendingPathComponent:@"dic.plist"];
   
    //可对已经存储的字典操作
    NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:documnetPath];
    NSLog(@"%@", documnetPath);

    /** 反面*/
    NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents1 = [array1 lastObject];
    NSString *documnetPath1 = [documents1 stringByAppendingPathComponent:@"sideBank.plist"];
    //准备要存入的字典
    //    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.bankCardUrl,@"bankCardUrl",nil];
    //可对已经存储的字典操作
    NSDictionary *resultDic1 = [NSDictionary dictionaryWithContentsOfFile:documnetPath1];


    /** 手持*/
    NSArray *array2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents2 = [array2 lastObject];
    NSString *documnetPath2 = [documents2 stringByAppendingPathComponent:@"holdBank.plist"];
    
    //可对已经存储的字典操作
    NSDictionary *resultDic2 = [NSDictionary dictionaryWithContentsOfFile:documnetPath2];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    NSString *cccc = [self.check.text substringToIndex:[self.check.text length] - 1];


    WYCLog(@"-----%@,%@,%@,%@,%@.%@.%@,%@,%@,%@,%@",[WYCAccountTool unarchiveuserName],[WYCAccountTool unarchiveidnumber],[resultDic objectForKey:@"cardNumber"],[resultDic objectForKey:@"bankId"],_chuan,self.CVN2TextField.text,self.iphoneTexField.text,[resultDic objectForKey:@"bankCardUrl"],[resultDic2 objectForKey:@"bankCardUrl"],cccc,self.QuotaTexField.text);



    NSDictionary * jsonStr = @{@"name":[WYCAccountTool unarchiveuserName],@"idNumber":[WYCAccountTool unarchiveidnumber],@"cardNumber":[resultDic objectForKey:@"cardNumber"],@"bankId":[resultDic objectForKey:@"bankId"],@"validityDate":_chuan,@"CVN2":self.CVN2TextField.text,@"mobile":self.iphoneTexField.text,@"faceUrl":[resultDic objectForKey:@"bankCardUrl"],@"backUrl":[resultDic1 objectForKey:@"bankCardUrl"],@"holdIdCardUrl":[resultDic2 objectForKey:@"bankCardUrl"],@"billDate":cccc,@"creditLine":self.QuotaTexField.text};




    NSError *error;

    NSData * data = [NSJSONSerialization dataWithJSONObject:jsonStr options:NSJSONWritingPrettyPrinted error:&error];

    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSString *pas = [RSAUtil encryptString:jsonString publicKey:RSA_P_KEY];

    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"request"] = pas;
    [HttpTool POST:creditcardaddCreditCard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            NSDictionary * dict = [responseObject objectForKey:@"result"];
            
            _cardId = dict[@"cardId"];
            [self loadNumber];
          
        }else{

            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
}
-(void)loadNumber
{
   
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardNumber"] = [WYCAccountTool unarchivecardNumber];
    [HttpTool POST:sendBindNumber params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            WYCCodeViewController  *codeView = [[WYCCodeViewController alloc]init];
            codeView.cardNumber = _cardNum;
            codeView.strName  =_bankName;
            codeView.strCard = _cardId;
            [self.navigationController pushViewController:codeView animated:YES];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer* bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"添加信用卡";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
